<?php


class ActivationParams {   // Alan writes
	var $load_threshold = 5;
	var $load_fanout_threshold = 1;
	var $spread_factor = 0.1;
	var $use_feedback_control = false;
	var $sigmoid_table;
    function __construct() {
		$this->sigmoid_table = array (
			array(0,0), array(5,1), array(10,2), array(20,5), array(30,12), array(40,27), array(50,50), array(60,73), array(70,88), array(80,95), array(90,98), array(95,99), array(100,100 )
		);
	}
	/*
	 *  internal helper function
	 *  calculates a sigmoid function (S shaped function capping large values and attenuating small ones and steep in the middle)
	 *  uses lookup table and interpolation
	 *  based on 0-100 activation levels
     * @param number x input raw activation
     * @return number the activation 'squashed' by the sigmoid
	 */
	function sigmoid($x) {
		$len = count($this->sigmoid_table);
		if ( $x <= $this->sigmoid_table[0][0] ) return $this->sigmoid_table[0][1];
		if ( $x >= $this->sigmoid_table[$len-1][0] ) return $this->sigmoid_table[$len-1][1];
		for( $i=1; $i<($len-1); $i++ ) {
			if ( $x <= $this->sigmoid_table[$i][0] ) break;
		}
		// fall through has $i = $len-1;
		$lambda = ( $x - $this->sigmoid_table[$i-1][0] ) / ( $this->sigmoid_table[$i][0] - $this->sigmoid_table[$i-1][0] );
		return ( (1-$lambda) * $this->sigmoid_table[$i-1][1] ) +  ( $lambda * $this->sigmoid_table[$i][1] );
	}
	/*
	 *  internal helper function
	 *  calculates inverse of sigmoid function above
	 *  based on 0-100 activation levels
     * @param number y desired output activation
     * @return number raw activation 'squashed' by the sigmoidthat would give $y
	 */
	function inverse_sigmoid($y) {
		$len = count($this->sigmoid_table);
		if ( $y <= $this->sigmoid_table[0][1] ) return $this->sigmoid_table[0][0];
		if ( $y >= $this->sigmoid_table[$len-1][1] ) return $this->sigmoid_table[$len-1][0];
		for( $i=1; $i<($len-1); $i++ ) {
			if ( $y <= $this->sigmoid_table[$i][1] ) break;
		}
		// fall through has $i = $len-1;
		$lambda = ( $y - $this->sigmoid_table[$i-1][1] ) / ( $this->sigmoid_table[$i][1] - $this->sigmoid_table[$i-1][1] );
		return ( (1-$lambda) * $this->sigmoid_table[$i-1][0] ) +  ( $lambda * $this->sigmoid_table[$i][0] );
	}
}



?>
